import * as assetTypeService from '../../services/assetType';

export default {
  namespace: 'assetType',
  state: {
    assetTypeTree: [],
    assetTypeList: [],
    current : 1,
    currentSize : 10,
    total : 0, 
    pid : 1,  
  },
  reducers: {
    queryAssetTypesSuc(state, { payload: assetTypeTree }) {
      return { ...state, assetTypeTree };
    },
    queryAssetTypesByPidSuc(state, { payload }) {
      return { ...state, ...payload };
    },
  },
  effects: {
    *queryAssetType({ payload },{ call, put }) {
      const { body }  = yield call(assetTypeService.queryAssetTypeRoot);
      yield put({ type: 'queryAssetTypesSuc', payload: body.data });
    },
    *queryAssetTypeByPid({ payload },{ call, put }) {
      const { body } = yield call(assetTypeService.queryAssetTypeByPid,payload);
      yield put({ type: 'queryAssetTypesByPidSuc', payload: { assetTypeList:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords, pid:payload.pid }});
    },
    *create({ payload ,search},{ call, put }){
      const { body } = yield call(assetTypeService.create,payload);
      yield put({ type: 'queryAssetType'});
      yield put({ type: 'queryAssetTypeByPid', payload:search});
    },
    *edit({ payload,search },{ call, put }){
      const { body } = yield call(assetTypeService.edit,payload);
      yield put({ type: 'queryAssetType'});
      yield put({ type: 'queryAssetTypeByPid', payload:search});
    },
    *remove({ payload ,search},{ call, put }){
      const { body } = yield call(assetTypeService.remove,payload);
       yield put({ type: 'queryAssetType'});
      yield put({ type: 'queryAssetTypeByPid', payload:search});
      yield put({ type: 'queryAssetType' });
    },
    *reload(action, { put }) {
      yield put({ type: 'queryAssetTypeByPid', payload:{ 'pid':1,'name':'' }});
      yield put({ type: 'queryAssetType' });
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/basic/assetType') {
          dispatch({ type: 'queryAssetType' });
          dispatch({ type: 'queryAssetTypeByPid', payload:{ 'pid':1,'name':'' }});
        }
      });
    },
  },
};
